package cn.wen.core.titlebar;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.wen.core.R;
import cn.wen.core.titlebar.base.BaseTitleBar;


/**
 * APP默认的通用标题
 */

public class DefTitleBar extends BaseTitleBar<DefBarBuild> {


    DefTitleBar(DefBarBuild build) {
        super(build);
    }


    @Override
    public int getBarLayoutId() {
        return R.layout.layout_title_bar;
    }

    @Override
    protected void applyParams(final DefBarBuild build) {
        if (build.mBgColor != 0) {
            findViewById(R.id.title_root)
                    .setBackgroundColor(build.mBgColor);
        }
        if (build.mLeftResId != 0) {
            ImageView mIvLeft = findViewById(R.id.m_bar_iv_left);
            mIvLeft.setImageResource(build.mLeftResId);
        }
        // 默认点击事件
        setOnClickListener(R.id.m_bar_iv_left, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (build.mActivity != null) {
                    build.mActivity.finish();
                }
            }
        });
        if (build.mLeftClickListener != null) {
            setOnClickListener(R.id.m_bar_iv_left, build.mLeftClickListener);
        }


        // 左边的View自定义的那种
        addLeftView(build.mLeftLayoutId);
        addLeftView(build.mLeftView);

        // 右边的View自定义的那种
        addRightView(build.mRightLayoutId);
        addRightView(build.mRightView);
        // 根据资源设置右边的图片和点击事件
        addRightIcons(build.mRightResIcons, build.mRightResClicks);


        // 设置Title的文本
        setText(R.id.m_bar_tv_title, build.mTitle);
        // 设置右边的文本
        setText(R.id.m_bar_tv_right, build.mRightText);
        if (build.mRightTextClickListener != null) {
            setOnClickListener(R.id.m_bar_tv_right, build.mRightTextClickListener);
        }
    }

    public DefTitleBar setTitle(CharSequence title) {
        // 设置Title的文本
        setText(R.id.m_bar_tv_title, title);
        return this;
    }

    public DefTitleBar setTitleLeftRightPadding(int dip) {
        // 设置Title的文本
        View title = findViewById(R.id.m_bar_tv_title);
        RelativeLayout parent = (RelativeLayout) title.getParent();
        parent.setPadding(dip2px(build.mActivity, dip), 0, dip2px(build.mActivity, dip), 0);
        return this;
    }

    public DefTitleBar setRightText(CharSequence text) {
        setText(R.id.m_bar_tv_right, text);
        return this;
    }

    public DefTitleBar setRightTextClick(View.OnClickListener listener) {
        setOnClickListener(R.id.m_bar_tv_right, listener);
        return this;
    }

    /**
     * 删掉返回按钮
     */
    public DefTitleBar clearBackClick() {
        // 设置Title的文本
        ImageView mIvLeft = findViewById(R.id.m_bar_iv_left);
        if (mIvLeft != null) {
            mIvLeft.setImageResource(android.R.color.transparent);
            mIvLeft.setOnClickListener(null);
        }

        return this;
    }

    /**
     * 添加左边的View
     */
    public DefTitleBar addLeftView(int leftResId) {
        if (leftResId != 0) {
            LinearLayout leftLayout = findViewById(R.id.m_bar_ll_left);
            leftLayout.removeAllViews();
            View leftView = LayoutInflater.from(build.mActivity).inflate(leftResId, leftLayout, false);
            leftLayout.addView(leftView);
        }
        return this;
    }

    /**
     * 添加左边的View
     */
    public DefTitleBar addLeftView(View leftView) {
        if (leftView != null) {
            LinearLayout leftLayout = findViewById(R.id.m_bar_ll_left);
            leftLayout.removeAllViews();
            leftLayout.addView(leftView);
        }
        return this;

    }

    /**
     * 添加右边的View
     */
    public DefTitleBar addRightView(int rightResId) {
        if (rightResId != 0) {
            LinearLayout rightLayout = findViewById(R.id.m_bar_ll_right);
            rightLayout.removeAllViews();
            View leftView = LayoutInflater.from(build.mActivity).inflate(rightResId, rightLayout, false);
            rightLayout.addView(leftView);
        }
        return this;
    }

    /**
     * 添加右边的View
     */
    public DefTitleBar addRightView(View rightView) {
        if (rightView != null) {
            LinearLayout rightLayout = findViewById(R.id.m_bar_ll_right);
            rightLayout.removeAllViews();
            rightLayout.addView(rightView);
        }
        return this;
    }

    public DefTitleBar setBgColor(int color) {
        findViewById(R.id.title_root).setBackgroundColor(color);
        return this;
    }

    /**
     * 添加右边的View
     */
    public DefTitleBar addRightIcons(int[] rightResIcons, View.OnClickListener[] rightResClicks) {
        if (rightResIcons != null && rightResIcons.length > 0) {
            LinearLayout rightLayout = findViewById(R.id.m_bar_ll_right);
            rightLayout.removeAllViews();
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ImageView imageView;
            int padding = dip2px(build.mActivity, 5);
            for (int i = 0; i < rightResIcons.length; i++) {
                imageView = new ImageView(build.mActivity);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                imageView.setImageResource(rightResIcons[i]);
                imageView.setPadding(padding, padding, padding, padding);
                // 设置点击事件
                if (rightResClicks != null && rightResClicks.length >= i) {
                    imageView.setOnClickListener(rightResClicks[i]);
                }
                rightLayout.addView(imageView, lp);
                imageView = null;
            }

        }
        return this;
    }

    public static int dip2px(Context context, int dip) {
        // 缩放比例(密度)
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5);
    }

    public void setRightTextColor(String color) {
        TextView mTvRight = findViewById(R.id.m_bar_tv_right);
        if (mTvRight != null && color != null) {
            mTvRight.setTextColor(Color.parseColor(color));
        }
    }



}
